package kkk.k;

import demo.controller.UserController;
import demo.dao.UserMapper;
import jdbc.InputUtil;
import jdbc.UserInfo;

import java.util.Scanner;

public class kk1 {
    private UserMapper userMapper=new UserMapper();
    public void start(){
        System.out.println("欢迎使用系统！请选择你要使用的功能：");
        System.out.println("[1]注册功能");
        System.out.println("[2]登录功能");
        System.out.println("[3]修改密码功能");
        Scanner scan=new Scanner(System.in);
        String num=scan.nextLine();
        switch (num){
            case "1":
                regUser();
                break;
            case "2":
                loginUser();
                break;
            case "3":
                changePWD();
                break;
            default:
                System.out.println("输入有误，请重新输入！");
        }
    }
    public void regUser(){
        UserInfo userInfo= InputUtil.getInputObject(new UserInfo(),"欢迎注册","注册");
        UserMapper userMapper=new UserMapper();
        if (userMapper.findByUsername(userInfo.getUsername())!=null){
            System.out.println("用户名已存在");
        }else{
            userMapper.insert(userInfo);
            System.out.println("注册成功");
        }



    }
    public void loginUser(){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=scan.nextLine();
        System.out.println("请输入密码:");
        String password=scan.nextLine();

        UserInfo userInfo=userMapper.findByUsername(username);
        if (userInfo==null||!userInfo.getPassword().equals(password)){
            System.out.println("用户名或密码不正确，登录失败！");
        }else{
            System.out.println("登录成功！");
        }

    }
    public void changePWD(){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=scan.nextLine();
        System.out.println("请输入旧密码：");
        String oldPassword=scan.nextLine();
        System.out.println("请输入新密码：");
        String newPassword=scan.nextLine();

        UserInfo userInfo=userMapper.findByUsername(username);
        if(userInfo==null||!userInfo.getPassword().equals(oldPassword)){
            System.out.println("用户名或密码不正确，修改失败！");
        }else {
            userInfo.setPassword(newPassword);
            userMapper.update(userInfo);
            System.out.println("修改成功");
        }
    }
    public static void main(String[] args) {
        UserController userController=new UserController();
        userController.start();
    }
}
